package c1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class FilteringApples {
    public static void main(String[] args) {
        //原始数据列表（库存）
        List<Apple> inventory= Arrays.asList(new Apple(100,"green"),
                                             new Apple(200,"green"),
                                             new Apple(300,"red"));
        //get all  green apples
        List<Apple> greenApples=filterApples(inventory,FilteringApples::isGreenApple);
        System.out.println(greenApples);

        //get all heavy apples
        List<Apple> heavyApples=filterApples(inventory,FilteringApples::isHeavyApple);
        System.out.println(heavyApples);

        //lambda get green
        List<Apple> greenApples2=filterApples(inventory,(Apple a)->"green".equals(a.getColor()));
        System.out.println(greenApples2);

        //lambda get heavy apples
        List<Apple> heavyApples2=filterApples(inventory,(Apple a)->a.getWeight()>150);
        System.out.println(heavyApples2);

        //stream get heavy apples
        List<Apple> heavyApples3=inventory.stream().filter(apple -> apple.getWeight()>150).collect(Collectors.toList());
        System.out.println(heavyApples3);

        //parallelStream get apples
        //利用多核cpu
        List<Apple> heavyApples4=inventory.parallelStream().filter(apple -> apple.getWeight()>150).collect(Collectors.toList());
        System.out.println(heavyApples4);


    }

    //旧的写法，取出green apple
    public static List<Apple> filterGreenApples(List<Apple> inventory){
        List<Apple> list=new ArrayList<>();
        for(Apple apple:inventory){
            if("green".equals(apple.getColor())){
                list.add(apple);
            }
        }
        return list;
    }

    //旧的写法，取出150以上重量的apple
    public static List<Apple> filterHeavyApples(List<Apple> inventory){
        List<Apple> list=new ArrayList<>();
        for(Apple apple:inventory){
            if(apple.getWeight()>150){
                list.add(apple);
            }
        }
        return list;
    }

    //传递方法，找出符合条件的apple
    public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p){
        List<Apple> list=new ArrayList<>();
        for(Apple apple:inventory){
            if(p.test(apple)){
                list.add(apple);
            }
        }
        return list;
    }

    public static boolean isGreenApple(Apple apple){
        return "green".equals(apple.getColor());
    }

    public static boolean isHeavyApple(Apple apple){
        return apple.getWeight()>150;
    }


    public static class Apple{
        private int weight=0;
        private String color="";

        public Apple(int weight, String color) {
            this.weight = weight;
            this.color = color;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        @Override
        public String toString() {
            return "Apple{color="+color+",weight="+weight+"}";
        }
    }
}
